package util

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

type config struct {
	Sip sipConfig `mapstructure:"sip"`
}

type sipConfig struct {
	IP        string `json:"ip" yaml:"ip"  mapstructure:"ip"`
	Port      int    `json:"port" yaml:"port"  mapstructure:"port"`
	NetType   string `json:"netType" yaml:"netType"  mapstructure:"netType"`
	Domain    string `json:"domain" yaml:"domain"  mapstructure:"domain"`
	UserAgent string `json:"userAgent" yaml:"userAgent"  mapstructure:"userAgent"`
	Expires   int    `json:"expires" yaml:"expires"  mapstructure:"expires"`
	Algorithm string `json:"algorithm" yaml:"algorithm"  mapstructure:"algorithm"`
	UserName  string `json:"userName" yaml:"userName"  mapstructure:"userName"`
	Password  string `json:"password" yaml:"password"  mapstructure:"password"`
	Qop       string `json:"qop" yaml:"qop"  mapstructure:"qop"`
}

var Conf = new(config)

func LoadConfig() {
	viper.SetConfigType("yml")
	viper.SetConfigName("config")
	viper.AddConfigPath("./")
	viper.SetDefault("sip.port", 5060)

	if err := viper.ReadInConfig(); err != nil {
		logrus.Panicln("配置文件缺失, err : ", err)
		panic(fmt.Errorf("配置文件缺失, err : ", err))
	}

	if err := viper.Unmarshal(Conf); err != nil {
		logrus.Panicln("配置文件读取错误, err : ", err)
		panic(fmt.Errorf("配置文件读取错误, err : ", err))
	}
}
